<?php
/**
*
* @package acp
* @version $Id: acp_express_pages.php,v 1.014 2008/03/09 15:35:00 nedka Exp $
* @copyright (c) 2008 nedka (Nguyen Dang Khoa)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* @package acp
*/
class acp_express_pages
{
	var $u_action;

	function main($id, $mode)
	{
		global $db, $user, $template;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;

		$user->add_lang('mods/express');

		// Set up general vars
		$action = request_var('action', '');
		$action = (isset($_POST['add'])) ? 'add' : $action;
		$action = (isset($_POST['save'])) ? 'save' : $action;
		$page_id = request_var('id', 0);

		$this->tpl_name = 'acp_express_pages';
		$this->page_title = 'ACP_EXPRESS_PAGES';

		$form_name = 'acp_express_pages';
		add_form_key($form_name);

		switch ($action)
		{
			case 'save':

				if (!check_form_key($form_name))
				{
					trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
				}

				$page_basename = utf8_normalize_nfc(request_var('page_basename', '', true));
				$page_name = utf8_normalize_nfc(request_var('page_name', '', true));
				$page_custom = request_var('page_custom', 0);
				$page_icon = request_var('page_icon', '');
				$page_content = ($page_custom) ? utf8_normalize_nfc(request_var('page_content', '', true)) : '';
				$template_file = ($page_custom) ? '' : request_var('template_file', '');
				$language_file = ($page_custom) ? '' : request_var('language_file', '');

				// The page icon has to be a jpg, gif or png
				if ($page_icon != '' && !preg_match('#(\.gif|\.png|\.jpg|\.jpeg)$#i', $page_icon))
				{
					$page_icon = '';
				}

				// The page template file has to be a htm, html or xml
				if (!$page_custom && $template_file != '' && !preg_match('#(\.htm|\.html|\.xml)$#i', $template_file))
				{
					$template_file = '';
				}

				// The page language file has to be a php
				if (!$page_custom && $language_file != '' && !preg_match('#(\.php)$#i', $language_file))
				{
					$language_file = '';
				}

				if (!$page_basename)
				{
					trigger_error($user->lang['NO_PAGE_BASENAME'] . adm_back_link($this->u_action), E_USER_WARNING);
				}

				if ($page_custom && $page_content == '')
				{
					trigger_error($user->lang['NO_PAGE_CONTENT'] . adm_back_link($this->u_action), E_USER_WARNING);
				}

				if (!$page_custom && ($template_file == '' || $language_file == ''))
				{
					trigger_error($user->lang['NO_PAGE_COMPONENT'] . adm_back_link($this->u_action), E_USER_WARNING);
				}

				$sql_ary = array(
					'page_basename'			=> strtolower($page_basename),
					'page_name'				=> $page_name,
					'page_custom'			=> $page_custom,
					'page_icon	'			=> htmlspecialchars_decode($page_icon),
					'page_content'			=> ($page_custom) ? $page_content : '',
					'page_content_uid'		=> '',
					'page_content_options'	=> 7,
					'page_content_bitfield'	=> '',
					'page_time'				=> time(),
					'template_file'			=> (!$page_custom) ? htmlspecialchars_decode($template_file) : '',
					'language_file'			=> (!$page_custom) ? htmlspecialchars_decode($language_file) : '',
				);

				$order_ary = array(
					'page_order'	=> $config['num_express_pages'] + 1,
				);

				// Get data for page content if specified
				if ($sql_ary['page_content'])
				{
					generate_text_for_storage($sql_ary['page_content'], $sql_ary['page_content_uid'], $sql_ary['page_content_bitfield'], $sql_ary['page_content_options'], request_var('content_parse_bbcode', false), request_var('content_parse_urls', false), request_var('content_parse_smilies', false));
				}

				if ($page_id)
				{
					$sql = 'UPDATE ' . EXPRESS_PAGES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE page_id = $page_id";
					$db->sql_query($sql);

					$message = $user->lang['PAGE_UPDATED'];

					add_log('admin', 'LOG_EXPRESS_PAGE_UPDATED', (!empty($user->lang[strtoupper($page_name)])) ? $user->lang[strtoupper($page_name)] : $page_name);
				}
				else
				{
					$sql = 'INSERT INTO ' . EXPRESS_PAGES_TABLE . ' ' . $db->sql_build_array('INSERT', array_merge($sql_ary, $order_ary));
					$db->sql_query($sql);

					$message = $user->lang['PAGE_ADDED'];

					set_config('num_express_pages', $config['num_express_pages'] + 1, true);
					add_log('admin', 'LOG_EXPRESS_PAGE_ADDED', (!empty($user->lang[strtoupper($page_name)])) ? $user->lang[strtoupper($page_name)] : $page_name);
				}

				trigger_error($message . adm_back_link($this->u_action));

			break;

			case 'delete':

				if (!$page_id)
				{
					trigger_error($user->lang['MUST_SELECT_PAGE'] . adm_back_link($this->u_action), E_USER_WARNING);
				}

				if (confirm_box(true))
				{
					$sql = 'SELECT page_name, page_order
						FROM ' . EXPRESS_PAGES_TABLE . "
						WHERE page_id = $page_id";
					$result = $db->sql_query($sql);
					$row = $db->sql_fetchrow($result);
					$db->sql_freeresult($result);

					if ($row)
					{
						$row['page_name'] = (string) $row['page_name'];
						$row['page_order'] = (int) $row['block_order'];
					}

					$sql = 'DELETE FROM ' . EXPRESS_PAGES_TABLE . " WHERE page_id = $page_id";
					$db->sql_query($sql);

					// Reset block order...
					$sql = 'UPDATE ' . EXPRESS_PAGES_TABLE . ' SET page_order = page_order - 1 WHERE page_order > ' . $row['page_order'];
					$db->sql_query($sql);

					set_config('num_express_pages', $config['num_express_pages'] - 1, true);
					add_log('admin', 'LOG_EXPRESS_PAGE_REMOVED', (!empty($user->lang[strtoupper($row['page_name'])])) ? $user->lang[strtoupper($row['page_name'])] : $row['page_name']);
				}
				else
				{
					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
						'i'			=> $id,
						'mode'		=> $mode,
						'page_id'	=> $page_id,
						'action'	=> 'delete',
					)));
				}

			break;

			case 'move_up':
			case 'move_down':

				if (!$page_id)
				{
					trigger_error($user->lang['MUST_SELECT_PAGE'] . adm_back_link($this->u_action), E_USER_WARNING);
				}

				// Get current order id...
				$sql = 'SELECT page_order AS current_order
					FROM ' . EXPRESS_PAGES_TABLE . "
					WHERE page_id = $page_id";
				$result = $db->sql_query($sql);
				$current_order = (int) $db->sql_fetchfield('current_order');
				$db->sql_freeresult($result);

				if ($current_order == 0 && $action == 'move_up')
				{
					break;
				}

				// on move_down, switch position with next order_id...
				// on move_up, switch position with previous order_id...
				$switch_order_id = ($action == 'move_down') ? $current_order + 1 : $current_order - 1;

				// Update order values
				$sql = 'UPDATE ' . EXPRESS_PAGES_TABLE . "
					SET page_order = $current_order
					WHERE page_order = $switch_order_id
						AND page_id <> $page_id";
				$db->sql_query($sql);

				// Only update the other entry too if the previous entry got updated
				if ($db->sql_affectedrows())
				{
					$sql = 'UPDATE ' . EXPRESS_PAGES_TABLE . "
						SET page_order = $switch_order_id
						WHERE page_order = $current_order
							AND page_id = $page_id";
					$db->sql_query($sql);
				}

			break;

			case 'edit':
			case 'add':

				include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);

				$user->add_lang('posting');

				// Assigning custom bbcodes
				display_custom_bbcodes();

				$pages = $existing_icons = $existing_htmls = $existing_langs = array();

				$sql = 'SELECT *
					FROM ' . EXPRESS_PAGES_TABLE . '
					ORDER BY page_order';
				$result = $db->sql_query($sql);

				while ($row = $db->sql_fetchrow($result))
				{
					$existing_icons[] = $row['page_icon'];
					$existing_htmls[] = $row['template_file'];
					$existing_langs[] = $row['language_file'];

					if ($action == 'edit' && $page_id == $row['page_id'])
					{
						$pages = $row;
					}
				}
				$db->sql_freeresult($result);

				// Select the page icon
				$icon_list = filelist($phpbb_root_path . $config['express_icons_path'], '');
				$edit_icon = $icon_filename_list = '';

				foreach ($icon_list as $path => $icon_ary)
				{
					sort($icon_ary);

					foreach ($icon_ary as $icon)
					{
						$icon = $path . $icon;

						if (!in_array($icon, $existing_icons) || $action == 'edit')
						{
							if ($pages && $icon == $pages['page_icon'])
							{
								$selected = ' selected="selected"';
								$edit_icon = $icon;
							}
							else
							{
								$selected = '';
							}

							if (strlen($icon) > 255)
							{
								continue;
							}

							$icon_filename_list .= '<option value="' . htmlspecialchars($icon) . '"' . $selected . '>' . $icon . '</option>';
						}
					}
				}

				$icon_filename_list = '<option value=""' . (($edit_icon == '') ? ' selected="selected"' : '') . '>' . $user->lang['SELECT_PAGE_ICON'] . '</option>' . $icon_filename_list;
				unset($existing_icons, $icon_list);

				// Select template fle for the page
				$html_list = filelist("{$phpbb_root_path}styles/" . $user->theme['template_path'] . '/template/custom_pages', '', 'htm|html|xml');
				$edit_html = $html_filename_list = '';

				foreach ($html_list as $path => $html_ary)
				{
					sort($html_ary);

					foreach ($html_ary as $html)
					{
						$html = $path . $html;

						if (!in_array($html, $existing_htmls) || $action == 'edit')
						{
							if ($pages && $html == $pages['template_file'])
							{
								$selected = ' selected="selected"';
								$edit_html = $html;
							}
							else
							{
								$selected = '';
							}

							if (strlen($html) > 255)
							{
								continue;
							}

							$html_filename_list .= '<option value="' . htmlspecialchars($html) . '"' . $selected . '>' . $html . '</option>';
						}
					}
				}

				$html_filename_list = '<option value=""' . (($edit_html == '') ? ' selected="selected"' : '') . '>' . $user->lang['SELECT_PAGE_HTML'] . '</option>' . $html_filename_list;
				unset($existing_htmls, $html_list);

				// Select language file for the page
				$lang_list = filelist("{$phpbb_root_path}language/" . $user->data['user_lang'] . '/mods/custom_pages', '', 'php');
				$edit_lang = $lang_filename_list = '';

				foreach ($lang_list as $path => $lang_ary)
				{
					sort($lang_ary);

					foreach ($lang_ary as $lang)
					{
						$lang = $path . $lang;

						if (!in_array($lang, $existing_langs) || $action == 'edit')
						{
							if ($pages && $lang == $pages['language_file'])
							{
								$selected = ' selected="selected"';
								$edit_lang = $lang;
							}
							else
							{
								$selected = '';
							}

							if (strlen($lang) > 255)
							{
								continue;
							}

							$lang_filename_list .= '<option value="' . htmlspecialchars($lang) . '"' . $selected . '>' . $lang . '</option>';
						}
					}
				}

				$lang_filename_list = '<option value=""' . (($edit_lang == '') ? ' selected="selected"' : '') . '>' . $user->lang['SELECT_PAGE_LANG'] . '</option>' . $lang_filename_list;
				unset($existing_langs, $lang_list);

				// Page content with BBCode support
				$page_content_data = array(
					'text'			=> (isset($pages['page_content'])) ? $pages['page_content'] : '',
					'allow_bbcode'	=> true,
					'allow_smilies'	=> true,
					'allow_urls'	=> true
				);

				$page_content_preview = '';

				// Parse page content if specified
				if (isset($pages['page_content']))
				{
					if (!isset($pages['page_content_uid']))
					{
						// Before we are able to display the preview and plane text, we need to parse our request_var()'d value...
						$pages['page_content_uid'] = '';
						$pages['page_content_bitfield'] = '';
						$pages['page_content_options'] = 0;

						generate_text_for_storage($pages['page_content'], $pages['page_content_uid'], $pages['page_content_bitfield'], $pages['page_content_options'], request_var('content_allow_bbcode', false), request_var('content_allow_urls', false), request_var('content_allow_smilies', false));
					}

					// Generate preview content
					$page_content_preview = generate_text_for_display($pages['page_content'], $pages['page_content_uid'], $pages['page_content_bitfield'], $pages['page_content_options']);

					// Decode...
					$page_content_data = generate_text_for_edit($pages['page_content'], $pages['page_content_uid'], $pages['page_content_options']);
				}

				$template->assign_vars(array(
					'ICONS_PATH'	=> $phpbb_root_path . $config['express_icons_path'],

					'PAGE_BASENAME'			=> (isset($pages['page_basename'])) ? $pages['page_basename'] : '',
					'PAGE_NAME'				=> (isset($pages['page_name'])) ? $pages['page_name'] : '',
					'PAGE_ICON'				=> ($edit_icon) ? $phpbb_root_path . $config['express_icons_path'] . '/' . $edit_icon : $phpbb_admin_path . 'images/spacer.gif',
					'PAGE_CONTENT'			=> $page_content_data['text'],
					'PAGE_CONTENT_PREVIEW'	=> $page_content_preview,

					'U_BACK'	=> $this->u_action,
					'U_ACTION'	=> $this->u_action . '&amp;id=' . $page_id,

					'S_EDIT'					=> true,
					'S_CUSTOM_PAGE'				=> (!isset($pages['page_custom']) || $pages['page_custom']) ? true : false,
					'S_ICON_FILENAME_LIST'		=> $icon_filename_list,
					'S_HTML_FILENAME_LIST'		=> $html_filename_list,
					'S_LANG_FILENAME_LIST'		=> $lang_filename_list,
					'S_CONTENT_BBCODE_CHECKED'	=> ($page_content_data['allow_bbcode']) ? true : false,
					'S_CONTENT_SMILIES_CHECKED'	=> ($page_content_data['allow_smilies']) ? true : false,
					'S_CONTENT_URLS_CHECKED'	=> ($page_content_data['allow_urls']) ? true : false,
				));

				return;

			break;
		}

		$template->assign_vars(array(
			'U_ACTION'	=> $this->u_action,
		));

		$sql = 'SELECT *
			FROM ' . EXPRESS_PAGES_TABLE . '
			ORDER BY page_order';
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$template->assign_block_vars('pages', array(
				'PAGE_BASENAME'	=> $row['page_basename'],
				'PAGE_NAME'		=> (!empty($user->lang[strtoupper($row['page_name'])])) ? $user->lang[strtoupper($row['page_name'])] : $row['page_name'],
				'PAGE_ICON'		=> $phpbb_root_path . $config['express_icons_path'] . '/' . $row['page_icon'],
				'PAGE_VIEWS'	=> $row['page_views'],

				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row['page_id'],
				'U_DELETE'		=> $this->u_action . '&amp;action=delete&amp;id=' . $row['page_id'],
				'U_MOVE_UP'		=> $this->u_action . '&amp;action=move_up&amp;id=' . $row['page_id'],
				'U_MOVE_DOWN'	=> $this->u_action . '&amp;action=move_down&amp;id=' . $row['page_id'],

				'S_PAGE_ICON'	=> ($row['page_icon']) ? true : false,
			));
		}
	}
}

?>